/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.jesseenglish.swingftfy.extensions;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.JPanel;

/**
 *
 * @author english1
 */
public class FPanel extends JPanel {

    private Image image;
    private boolean repeat;
    private Color heldBackground;

    public FPanel() {
        this.image = null;
        this.repeat = false;
        super.setBackground(new Color(1.0f, 1.0f, 1.0f, 0.0f));
        this.heldBackground = Color.LIGHT_GRAY;
    }

    @Override
    public void setBackground(Color color) {
        this.heldBackground = color;
        return;
    }

    public void setImage(Image image) {
        this.image = image;
    }

    public void setRepeat(boolean repeat) {
        this.repeat = repeat;
    }

    @Override
    public void paint(Graphics g) {
        int width = this.getWidth();
        int height = this.getHeight();

        g.setColor(this.heldBackground);
        g.fillRect(0, 0, width, height);

        if (this.image == null) {
            super.paint(g);
            return;
        }

        int patternWidth = this.image.getWidth(this);
        int patternHeight = this.image.getHeight(this);

        if (repeat) {
            for (int i = 0; i < width; i += patternWidth) {
                for (int j = 0; j < height;  j += patternHeight) {
                    g.drawImage(this.image, i, j, this);
                }
            }
        } else {
            int x = (width / 2) - (patternWidth / 2);
            int y = (height / 2) - (patternHeight / 2);
            g.drawImage(this.image, x, y, this);
        }

        super.paint(g);
    }

}
